<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ include file="/WEB-INF/views/common.jsp"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<script type="text/javascript">
	$(function() {
		var examDatagrid = $("#examDatagrid");
		var examDialog = $("#examDialog");
		var addForm = $("#addForm");
		var cmdObject = {
			addExam : function() {
				examDialog.dialog('open').dialog('setTitle', '新增考试');
				addForm.form('clear');
				$("#error").text("");
			},
			deleteExam : function() {
				var row = examDatagrid.datagrid('getSelected');
				if (!row) {
					$.messager.alert('提示', '删除前请选中一行', 'info');
					return;
				}
				$.messager.confirm('提示', '确定要删除这条记录？', function(r) {
					if (r) {
						$.get("/exam/delete?id=" + row.id, function(data) {
							if (data.code == 0) {
								//重新刷新Datagrid
								examDatagrid.datagrid('reload');
							} else {
								$.messager.alert('提示', data.msg, 'error');
							}
						});
					}
				})
			},
			saveExam : function() {
				var radioNum = $("#radioNum").val();
				var radioScore = $("#radioScore").val();
				var checkNum = $("#checkNum").val();
				var checkScore = $("#checkScore").val();
				var judgeNum = $("#judgeNum").val();
				var judgeScore = $("#judgeScore").val();
				if((radioNum * radioScore + checkNum * checkScore + judgeNum*judgeScore)!=100) {
					$("#error").text("所有题的总分需为100！");
					return ;
				}
				addForm.form('submit',{
					url : "/exam/save",
					onSubmit : function() {
						return $(this).form('validate');
					},
					success : function(data) {
						var data = JSON.parse(data);
						if(data.code == 0) {
							//重新刷新departmentDatagrid
							examDatagrid.datagrid('reload');
							examDialog.dialog('close');	
						} else {
							$.messager.alert('提示', data.msg, 'error');
						}
					}
				})
			},
			cancelExam : function() {
				examDialog.dialog('close');
			}
			
		}

		$("a[data-cmd]").on('click', function() {
			var cmd = $(this).data("cmd");
			if (cmd && !$(this).hasClass('l-btn-disabled')) {
				cmdObject[cmd]();
			}
		});

	});

	function timeFormat(value, row, index) {
		var date = new Date(value);
		return date.format("yyyy-MM-dd hh:mm:ss");
	}

	function examTimeFormat(value, row, index) {
		return value + '分钟';
	}

	function stateFormat(value, row, index) {　/* 标准格式，应使用枚举 */
		var result = '';
		if (value == 0) {
			result = '<font color="red">未开考</font>';
		} else if (value == 1) {
			result = '<font color="red">正在考试</font>';
		} else
			result = '已结束';
		return result;
	}
	
	function canAgainFormat(value, row, index) {
		if(value == 0) {
			return '可以';
		} else {
			return '禁止';
		}
	}

	/** 
	 *时间格式转换，copy */
	Date.prototype.format = function(format) {
		var o = {
			"M+" : this.getMonth() + 1, //month 
			"d+" : this.getDate(), //day 
			"h+" : this.getHours(), //hour 
			"m+" : this.getMinutes(), //minute 
			"s+" : this.getSeconds(), //second 
			"q+" : Math.floor((this.getMonth() + 3) / 3), //quarter 
			"S" : this.getMilliseconds()
		//millisecond 
		}
		if (/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear() + "")
					.substr(4 - RegExp.$1.length));
		}
		for ( var k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
						: ("00" + o[k]).substr(("" + o[k]).length));
			}
		}
		return format;
	}
</script>
<body>
	<table id="examDatagrid" class="easyui-datagrid" toolbar="#examTooBar"
		url="/exam/list" pagination="true" fit="true" fitColumns="true"
		singleSelect="true" striped="true">
		<thead>
			<tr>
				<th field="id" align="center" width="40">编号</th>
				<th field="examName" align="center" width="80">名称</th>
				<th field="createTime" align="center" width="70"
					formatter="timeFormat">创建时间</th>
				<th field="examTime" align="center" width="50"
					formatter="examTimeFormat">考试时间</th>
				<th field="state" align="center" width="50" formatter="stateFormat">状态</th>
				<th field="radioNum" align="center" width="50">单选个数(/个)</th>
				<th field="radioScore" align="center" width="50">单选分值(/分)</th>
				<th field="checkNum" align="center" width="50">多选个数(/个)</th>
				<th field="checkScore" align="center" width="50">多选分值(/分)</th>
				<th field="judgeNum" align="center" width="50">判断个数(/个)</th>
				<th field="judgeScore" align="center" width="50">判断分值(/分)</th>
				<th field="pass" align="center" width="50">及格线</th>
				<th field="againe" align="center" width="50" formatter="canAgainFormat">补考</th>
			</tr>
		</thead>
	</table>
	<div id="examTooBar">
		<a id="add" href="javascript:void(0)" data-cmd="addExam"
			class="easyui-linkbutton" iconCls="icon-add">新增考试</a> <a id="delete"
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" data-cmd="deleteExam">删除考试 </a>
	</div>
	<div id="examDialog" class="easyui-dialog"
		style="width: 300px; height: 370px; padding: 10px 20px" closed="true"
		buttons="#DialogButtons">
		<form id="addForm" class="fm" method="post" novalidate>
			<!-- 没有编辑，不考虑回显 -->
			<table>
				<tr>
					<div class="fitem">
						<td><label>考试名:</label></td>
						<td><input name="examName" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>考试耗时:</label></td>
						<td><select class="easyui-combobox" name="examTime"
							style="width: 100px;">
								<option value="1">1分钟（测试用）</option>
								<option value="30">30分钟</option>
								<option value="60">60分钟</option>
								<option value="90">90分钟</option>
								<option value="120">120分钟</option>
						</select></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>单选个数:</label></td>
						<td><input id="radioNum" name="radioNum" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>单选分数:</label></td>
						<td><input id="radioScore" name="radioScore" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>多选个数:</label></td>
						<td><input id="checkNum" name="checkNum" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>多选分数:</label></td>
						<td><input id="checkScore" name="checkScore" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>判断个数:</label></td>
						<td><input id="judgeNum" name="judgeNum" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>判断分数:</label></td>
						<td><input id="judgeScore" name="judgeScore" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>及格分数线:</label></td>
						<td><input id="pass" name="pass" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>补考:</label></td>
						<td><select class="easyui-combobox" name="againe"
							style="width: 100px;">
								<option value="0">允许</option>
								<option value="1">禁止</option>
						</select></td>
					</div>
				</tr>
			</table>
			<span id="error"
				style="margin-left: 55px; color: red; font-size: 14px; float: left"></span>
		</form>
	</div>
	<div id="DialogButtons">
		<a id="save" data-cmd="saveExam" href="javascript:void(0)"
			class="easyui-linkbutton" iconCls="icon-ok">新增</a> <a id="cancel"
			data-cmd="cancelExam" href="javascript:void(0)"
			class="easyui-linkbutton" iconCls="icon-cancel">取消</a>
			<input type="hidden" value="@author:13级计算机科学与技术1班   王宇民">
	</div>
</body>
</html>